package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.HourWageBean;
import flc.ast.databinding.ActivityAddHourWageBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.utils.Str2NumUtil;
import stark.common.basic.utils.ToastUtil;

public class AddHourWageActivity extends BaseAc<ActivityAddHourWageBinding> {

    public static List<HourWageBean> listWage;
    public static int selPosition;
    public static int type;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_hour_wage;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddHourWageBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivConfirm.setOnClickListener(this);

        mDataBinding.etAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String amount = s.toString();
                String name = mDataBinding.etName.getText().toString().trim();
                if (TextUtils.isEmpty(amount) || TextUtils.isEmpty(name)) {
                    mDataBinding.ivConfirm.setImageResource(R.drawable.wqd);
                } else {
                    mDataBinding.ivConfirm.setImageResource(R.drawable.yqd);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mDataBinding.etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                String amount = mDataBinding.etAmount.getText().toString().trim();
                String name = s.toString();
                if (TextUtils.isEmpty(amount) || TextUtils.isEmpty(name)) {
                    mDataBinding.ivConfirm.setImageResource(R.drawable.wqd);
                } else {
                    mDataBinding.ivConfirm.setImageResource(R.drawable.yqd);
                }
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivConfirm://确定
                String amount = mDataBinding.etAmount.getText().toString().trim();
                String name = mDataBinding.etName.getText().toString().trim();
                if (TextUtils.isEmpty(amount) || TextUtils.isEmpty(name)) return;
                if (type == 0) {
                    listWage.get(selPosition).setName(name);
                    listWage.get(selPosition).setWage(Integer.parseInt(amount));
                    AppSpUtils.saveWageList(listWage);
                } else {
                    List<HourWageBean> listSp = AppSpUtils.getWageList();
                    listSp.add(new HourWageBean(name, Str2NumUtil.parseInt(amount, 0)));
                    AppSpUtils.saveWageList(listSp);
                }
                ToastUtils.showShort(getString(R.string.save_success));
                finish();
                break;

        }
    }

    @Override
    protected void initData() {
        if (type == 0) {
            mDataBinding.tvAddHourWageTitle.setText(getString(R.string.edit_hour_wage));
            mDataBinding.etAmount.setText(listWage.get(selPosition).getWage() + "");
            mDataBinding.etName.setText(listWage.get(selPosition).getName());
        } else {
            mDataBinding.tvAddHourWageTitle.setText(getString(R.string.add_hour_wage));
        }
    }
}